﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// ===============================================================================

using System;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统字典管理
    /// </summary>
    [FormCaption("系统字典管理")]
    public partial class FrmDicManager : BaseForm
    {
        private readonly bool allowInsert = SysApp.Permit("sys.dicCategory.insert");
        private readonly bool allowUpdate = SysApp.Permit("sys.dicCategory.update");
        private readonly bool allowDelete = SysApp.Permit("sys.dicCategory.delete");
        private readonly bool allowExport = SysApp.Permit("sys.dicCategory.export");

        public FrmDicManager()
        {
            InitializeComponent();
            tree.ModelType = typeof(SysDicCategory);
        }

        /// <summary>
        /// 数据编辑
        /// </summary>
        /// <param name="param">数据编辑参数</param>
        private void ShowEditForm(EditFormArgs param)
        {
            var editForm = new FrmDicCategoryEdit(param);
            editForm.ShowDialogOk(async () => await tree.LoadAsync());
        }

        private async void FrmDicManager_Load(object sender, EventArgs e)
        {
            tree.StateImageList = SysDicService.Instance.GetDicCategoryImageCollection();
            await tree.LoadAsync();
            tree.ExpandAll();
        }

        private void BtnNewRootDicCategory_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowEditForm(EditFormArgs.CreateTree(R.RootTreeId));
        }

        private void BtnNewDicCategory_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowEditForm(EditFormArgs.CreateTree(tree.SelectedId));
        }

        private void BtnEditDicCategory_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowEditForm(EditFormArgs.EditTree(tree.SelectedId, ((SysDicCategory)tree.Selected).ParentId));
        }

        private async void BtnCopyCategory_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要复制当前选中的数据吗?") != System.Windows.Forms.DialogResult.Yes) return;
            await SysDicService.Instance.CopyDicCategoryAsync(tree.SelectedId);
            await tree.LoadAsync();
        }

        private async void BtnDeleteDicCategory_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            await tree.DeleteSelectedAsync();
        }

        private async void BtnRefreshDicCategory_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            await tree.LoadAsync();
        }

        private void Tree_LoadData(object sender, LoadTreeDataEventArgs e)
        {
            e.Task = async () => await SysDicService.Instance.SelectDicCategoryListAsync(SysDicCategoryFilter.Default);
        }

        private async void Tree_NodeDragChangedChanged(object sender, NodeDragChangedEventArgs e)
        {
            await SysDicService.Instance.UpdateDicCategoryDndAsync(e.ChangedData);
        }

        private void Tree_DeleteData(object sender, DeleteDataEventArgs e)
        {
            e.Task = () => SysDicService.Instance.DeleteDicCategoryAsync(tree.SelectedId);
        }

        private async void Tree_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tree.Selected is SysDicCategory dicCategory)
            {
                dicManagerGroup.Text = $"{dicCategory.Name} - {dicCategory.Code}";
                await dicDetailsManager.SetCategoryAsync(dicCategory);
            }
        }

        private void Tree_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
        {
            e.NodeImageIndex = SysDicService.Instance.GetDicNodeImageIndex(e.Node);
        }

        private void Tree_RowSelectionChanged(object sender, EventArgs e)
        {
            btnNewRootDicCategory.Enabled = allowInsert;
            btnNewDicCategory.Enabled = allowInsert;
            btnEditDicCategory.Enabled = tree.HasSingleSelected && allowUpdate;
            btnCopyDicCategory.Enabled = tree.HasSingleSelected && allowInsert;
            btnDeleteDicCategory.Enabled = tree.HasSelected && allowDelete;
            btnExportDicCategory.Enabled = tree.HasData && allowExport;
        }

    }
}